GBML manual / Maths

angle_diff

The signed SHORTEST turn from angle from to angle to, in degrees - positive is counterclockwise, negative is clockwise, and the result is always in (-180, 180].

Syntax

angle_diff(from, to) -> real

Arguments

NameType
fromreal
toreal

Returns

real

Description

The signed shortest turn from angle from to angle to, in degrees - positive is counterclockwise, negative is clockwise, and the answer always lands in (-180, 180] (-180 itself is never returned, but +180 is). angle_diff answers "which side, and how far" in one call - it wraps correctly no matter how from/to are spelled, so neither one needs to already be inside 0-360 first.

Example

target = instance_nearest(x, y, obj_doc_walker);
if (target != nothing) {
    var turn = angle_diff(direction, dir_to(x, y, target.x, target.y));
}

How far, and which way, this turret would need to turn to face its nearest target - a positive turn means counterclockwise, negative means clockwise. angle_diff only measures the turn here; it doesn't actually turn anything by itself - see angle_approach for the version that does.

The 359°→1° crossing

angle_diff(359, 1) is 2, not -358 - it always finds the short way round the circle, never the long way, no matter which side of 0/360 either angle happens to fall on.

Exactly 180° away - a real tie, resolved on purpose

When from and to are exactly opposite, a half turn either direction is equally short, and there is genuinely no "more correct" side to prefer. angle_diff always answers +180 in that case, never -180 - a deliberate, documented choice so the answer is at least consistent every time it comes up, including inside angle_approach, which turns positive (counterclockwise) in that exact tie.

Cost

One divide (folding the difference into a 0-360 range first) plus a comparison - the same class of cost angle_tan already carries, not a new one.

On the GBA

Wraps correctly for any input - from/to never need to already be in 0-360 first. 359 to 1 answers 2, not -358: it always finds the SHORT way round, never the long way. Exactly 180 degrees away (the one case with no shorter side) always answers +180, never -180 - a deliberate, documented tie-break, not an arbitrary one, and angle_approach below inherits it. Standing on the same angle answers 0.

See also: angle_approachdir_todirection